/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.jgls.bi.dataprocess;

/**
 *
 * @author Administrator
 */
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultReader;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import javax.sql.DataSource;

public class biProvidBalanceJdbcDao extends JdbcDaoSupport implements biProvidBalanceDataDao {

    @SuppressWarnings({"static-access", "empty-statement"})
    @Override
    public void biProvidBalanceData(Date adTsnDate, int aiShopId, String asShopBm,
            int aiDateNo, String asYear, int aiYybh, int aiMan_id, String asTname) {
        int iShopCnt = 0;
        int iRc = 0;
        int aiYear = 0, aiWeek = 0;
        int i = 0;
        final int isize;
        final int isize2;
        final int iyear,  iweek;
        final Date dd;
        int ibal;
        double dAmt = 0;
        double dValue = 0;
        int isup_id = 0;
        int isup_id2 = 0;
        int iperiod = 0;
        //该程序只运行一次即可，所以目前定为2店，如2店闭店，请换为其他门店
        if (aiShopId != 2) {
            return;
        }
        String sql = "";
        String sLb = "";
        String sDate = "";
        String sCcode = "";
        int iYybh = 0;
        ArrayList<Integer> aiSC = new ArrayList<Integer>();

        final ArrayList<Integer> aiSC2 = new ArrayList<Integer>();
        final ArrayList<Integer> aiBal2 = new ArrayList<Integer>();
        final ArrayList<Double> adAmt = new ArrayList<Double>();

        List lstbiStock;
        List lstbiStockYyb;
        sDate = new SimpleDateFormat("w").format(adTsnDate);
        aiWeek = Integer.parseInt(sDate);
        //cal.set(iRc, iRc, iRc);

        System.out.println("now processing    biProvidBalance's Data... Date:" + adTsnDate +
                " shopid;" + aiShopId + " please wait...");
        aiYear = Integer.parseInt(asYear);
        //取总销售金额
        JdbcTemplate jt = getJdbcTemplate();
        sql = "select sup_id  from sy_archi_provid " +
                "where sup_id > 1040000 and sup_id < 2100000 order by sup_id desc";
        Object[] params = new Object[]{adTsnDate};
        int[] types = {Types.DATE};
        lstbiStock = jt.query(sql, new RowMapperResultReader(new biRM()));

        Iterator Iter = lstbiStock.iterator();
        biProvidBalance ltam = new biProvidBalance();
        while (Iter.hasNext()) {
            ltam = (biProvidBalance) Iter.next();
            isup_id = ltam.getSup_id();
            aiSC.add(isup_id);
            //cls
            isup_id = 0;
        }

        //begin insert into biStock
        //1:delete
        sql = "delete from t_bi_provid_balance where tsn_date = ? ";
        iRc = jt.update(sql, params, types);
        //2:insert

        iyear = aiYear;
        iweek = aiWeek;
        dd = adTsnDate;
        //总的
        isize = aiSC.size();
        //从用友中取数据
        ApplicationContext ctx2 = new ClassPathXmlApplicationContext("SpringConfig_Jdbc.xml");
        DataSource ds2 = (DataSource) ctx2.getBean("SqlServer");
        U8JdbcDao taskDao = new U8JdbcDao();
        taskDao.setDataSource(ds2); //取用友中的数据

        for (i = 0; i < isize; i++) {
            aiSC2.add(aiSC.get(i));
            //判断厂编是否为104开头，如果为104开头，则转化为205开头
            if (aiSC.get(i) >= 1040001 && aiSC.get(i) <= 1049999) {
                isup_id2 = aiSC.get(i) + 1010000;
            } else {
                isup_id2 = aiSC.get(i);
            }
            //利用厂编作为科目编号从用友中获取科目余额
            sCcode = isup_id2 + "";
            Calendar c = Calendar.getInstance();
            iperiod = c.get(Calendar.MONTH) + 1;
            taskDao.setStrCcode(sCcode);
            taskDao.setIperiod(iperiod);
            ibal = taskDao.getBalance();
            aiBal2.add(ibal);
            //cls
            ibal=0;
            //sleep
            try{
                 Thread.currentThread().sleep(100);;//1000ms   =   1sec
              }catch(InterruptedException   e){
                  e.printStackTrace();
              }
        }
        if (isize > 0) {

            sql = "insert into t_bi_provid_balance " +
                    "(tsn_date,sup_id,balance_amt)" +
                    " values(?,?,?)";
            BatchPreparedStatementSetter setter = null;
            setter = new BatchPreparedStatementSetter() {

                public int getBatchSize() {
                    return isize;
                }

                public void setValues(PreparedStatement ps, int index) throws SQLException {
                    ps.setDate(1, dd);
                    ps.setInt(2, aiSC2.get(index));
                    ps.setInt(3, aiBal2.get(index));
                }
            };
            jt.batchUpdate(sql, setter);
        }
    }

    class biRM implements RowMapper {

        public Object mapRow(ResultSet rs, int index)
                throws SQLException {
            biProvidBalance lmx = new biProvidBalance();
            lmx.setSup_id(rs.getInt("sup_id"));
            return lmx;
        }
    }
}
